
//
//	cove.events.EventDispatcher - 2011 26th June
//
//	The EventDispatcher class implements the IEventDispatcher interface and is the base class for
//	the DisplayObject class. The EventDispatcher class allows any object on the display list to be
//	an event target and as such, to use the methods of the IEventDispatcher interface.  
//
//-------------------------------------------------------------------------------------------------

var EventDispatcher = cove.events.EventDispatcher = Class.extend({


//	Public methods
//-------------------------------------------------------------------------------------------------

	// Registers an event listener object with an EventDispatcher object so that the listener
	// receives notification of an event.
	addEventListener: function(type, listener, useCapture, priority, useWeakReference) {
		if(typeof useCapture == 'boolean') useCapture = false;
		if(typeof priority == 'number') priority = 0;
		if(typeof useWeakReference == 'boolean') useWeakReference = false;
	},
	
	// Dispatches an event into the event flow.
	dispatchEvent: function(event) {
		var dispatched = false;
		
		return dispatched;
	},
	
	// Checks whether the EventDispatcher object has any listeners registered for a specific type
	// of event.
	hasEventListener: function(type) {
		
	},
	
	// Removes a listener from the EventDispatcher object.
	removeEventListener: function(type, listener, useCapture) {
		
	},
	
	// Checks whether an event listener is registered with this EventDispatcher object or any of
	// its ancestors for the specified event type.
	willTrigger: function(type) {
		// Bubble up the display object chain and check each parent for the event listener
		//return Boolean
	},
	
	// Returns a string representation of the class
	toString: function() {
		return '[object EventDispatcher]';
	}
	
});




/*cove.events.EventDispatcher = Class.extend(function() {
	
	priv.eventListeners = {};
	priv.defaultEventListeners = {}; // Just one per event type


//	Public methods
//-------------------------------------------------------------------------------------------------

	// Registers an event listener object with an EventDispatcher object so that the listener
	// receives notification of an event.
	this.addEventListener = function(type, listener, useCapture, priority, useWeakReference) {
		if(typeof useCapture == 'boolean') useCapture = false;
		if(typeof priority == 'number') priority = 0;
		if(typeof useWeakReference == 'boolean') useWeakReference = false;
		
		if(!(priv.eventListeners[type] instanceof Array))
			priv.eventListeners[type] = [];
		
		priv.eventListeners[type].push({
			listener: listener,
			useCapture: useCapture,
			priority: priority,
			useWeakReference: useWeakReference
		})
	};
	
	// Dispatches an event into the event flow.
	this.dispatchEvent = function(event) {
		var dispatched = false;
		if(event instanceof cove.events.Event) {
			var eventPriv = privateKey(event);
			if(!eventPriv.jsDefaultCall) {
				var r = false;
				if(priv.jsObject instanceof HTMLElement/* && event instanceof cove.events.MouseEvent* /) {
					r = true;
					switch(event.type) {
						case 'click':
							
						break;
						default:
							r = false;
						break;
					}
				}
				if(r) return;
			}
			var el = priv.eventListeners[event.type], propagate = true, prevent = false;
			if(el instanceof Array) {
				el.sort(function(a, b) { return b.priority - a.priority; });
				var i, len = el.length, newEvent;
				if(len) dispatched = true;
				for(i = 0; i < len; i++) {
					newEvent = event.clone();
					el[i].listener(newEvent);
					eventPriv = privateKey(newEvent);
					propagate = propagate && eventPriv.propagation;
					prevent = prevent || eventPriv.defaultPrevented;
					if(!eventPriv.immediatePropagation) {
						break;
					}
				}
			}
			if(!prevent && typeof priv.defaultEventListeners[event.type] == 'object') {
				var obj = priv.defaultEventListeners[event.type];
				obj.listener(new cove.events.Event(event.type, obj.bubbles, obj.cancelable));
			}
			//if this instanceof DisplayObject
			// if propagation
			//  parent dispatchEvent
			eventPriv = privateKey(event);
			eventPriv.propagation = propagate;
			eventPriv.defaultPrevented = prevent;
		}
		return dispatched;
	};
	
	// Checks whether the EventDispatcher object has any listeners registered for a specific type
	// of event.
	this.hasEventListener = function(type) {
		return typeof type == 'string' && (
			priv.eventListeners[type] instanceof Array &&
			priv.eventListeners[type].length
		);
	};
	
	// Removes a listener from the EventDispatcher object.
	this.removeEventListener = function(type, listener, useCapture) {
		if(this.hasEventListener(type) && listener instanceof Function) {
			if(typeof useCapture == 'boolean') useCapture = false;
			var el = priv.eventListeners[type], i = el.length;
			while(--i >= 0) {
				if(el[i].listener == listener && el[i].useCapture == useCapture) {
					el[i].splice(i, 1)
					break;
				}
			}
		}
	};
	
	// Checks whether an event listener is registered with this EventDispatcher object or any of
	// its ancestors for the specified event type.
	this.willTrigger = function(type) {
		// Bubble up the display object chain and check each parent for the event listener
		//return Boolean
	};
	
	// Returns a string representation of the class
	this.toString = function() {
		return '[object EventDispatcher]';
	};
	
});*/
